//
//  SEGWorkOrderAreaViewController.m
//  Uhomebk
//
//  Created by Chiang on 2017/11/17.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "SEGWorkOrderAreaViewController.h"
#import "AreaCell.h"

#define kCellIdentifier @"kCellIdentifier"

@interface SEGWorkOrderAreaViewController ()<UITableViewDelegate,UITableViewDataSource>{
    UITableView *tableView;
}

@end

@implementation SEGWorkOrderAreaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self showCustomNavigationBackButton];
    tableView = [self newTableViewWithFrame:CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight() - kNavigationBarAndStatusBarHeight)];
    [self.view addSubview:tableView];
}

- (void)setDefaultValue:(NSString *)defaultValue {
    _defaultValue = defaultValue;
    for (NSDictionary *dic in _dataArray) {
        if ([_defaultValue isEqualToString:[SEGUtils ValueToString:dic[@"areaId"]]]) {
            [self setSelectedStatus:dic];
        } else {
            [self setUnSelectedStatus:dic];
        }
    }
}

#pragma mark - UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AreaCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
    cell.selectDic = _dataArray[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *selectDic = _dataArray[indexPath.row];
    for (NSDictionary *dic in _dataArray) {
        if (selectDic == dic) {
            [self setSelectedStatus:dic];

        } else {
            [self setUnSelectedStatus:dic];
        }
    }
    [self.fundelegate returnSelectedData:selectDic];
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - Methods
/**
 设置为选中状态，选择的数据
 */
- (void)setSelectedStatus:(NSDictionary *)dic {
    [dic setValue:@"1" forKey:@"sel"];
}

/**
 设置为未选中状态，选择的数据
 */
- (void)setUnSelectedStatus:(NSDictionary *)dic {
    [dic setValue:@"0" forKey:@"sel"];
}

/**
 获取选中的值
 */
- (NSDictionary *)getSelectedData {
    NSDictionary *selectedDic = nil;
    for (NSDictionary *dic in _dataArray) {
        if ([dic[@"sel"] isEqualToString:@"1"]) {
            selectedDic = dic;
            break;
        }
    }
    return selectedDic;
}

- (void)returnSelectedData:(id)data {
    NSDictionary *dic = data;
    [self.fundelegate returnSelectedData:dic];
}

#pragma mark - UI控件
- (UITableView *)newTableViewWithFrame:(CGRect)frame {
    UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
    [tableView setSeparatorColor:kColor_L2];
    [tableView setDelegate:self];
    [tableView setDataSource:self];
    [tableView setBackgroundColor:kColor_W];
    [tableView registerClass:[AreaCell class] forCellReuseIdentifier:kCellIdentifier];
    tableView.tableFooterView = [[UIView alloc] init];
    return tableView;
}

@end
